<template>
  <el-upload
  class="avatar-uploader"
  :show-file-list="false"
  :before-upload="beforeUpload"
>
  <el-image v-if="props.picture" :src="props.picture" class="avatar" width="100%" />
  <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
</el-upload>
</template>

<script setup>
import {uploadFile} from '@/api/file.js';
import {defineProps,defineEmits} from 'vue';
import { Plus } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
// 定义属性值
const props = defineProps({
  picture: {
    type: String
  }
})

const emit = defineEmits(["update:picture"]);

// 上传头像
const beforeUpload = async (file)=> {
  const response = await uploadFile(file);
  const url = response.data.data
  console.log(url);
  emit("update:picture", url);
  ElMessage({
    type: 'success',
    message: "上传成功"
  })
  // 返回false，使用自定义的上传方法
  return false;
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>